package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.mapper.*;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatsCacheService;
import cn.wolfcode.luowowo.cache.vo.StrategyStats;
import cn.wolfcode.luowowo.common.query.QueryObject;
import cn.wolfcode.luowowo.common.query.StrategyQueryObject;
import cn.wolfcode.luowowo.common.query.StrategyQueryObject;
import cn.wolfcode.luowowo.common.query.TravelQueryObject;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService {
    @Autowired
    private StrategyDetailMapper strategyDetailMapper;
    @Autowired
    private StrategyContentMapper contentMapper;
    @Autowired
    private StrategyTagMapper strategyTagMapper;
    @Autowired
    private StrategyCatalogMapper strategyCatalogMapper;
    @Autowired
    private UserStrategyMapper userStrategyMapper;
    @Reference
    private IStrategyStatsCacheService strategyStatsCacheService;

    public PageInfo<StrategyDetail> query(StrategyQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<StrategyDetail> list = strategyDetailMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    public StrategyDetail get(Long id) {
        //1.先查询文章
        StrategyDetail strategyDetail = strategyDetailMapper.selectByPrimaryKey(id);
        //2.查询文章内容
        StrategyContent content = contentMapper.selectByPrimaryKey(id);
        strategyDetail.setStrategyContent(content);
        return strategyDetail;
    }

    public void saveOrUpdate(StrategyDetail entity, String tags) {
        //1.处理文章内容
        StrategyContent content = entity.getStrategyContent();

        //生成摘要,模拟AI生成的
        entity.setSummary("人工智能生成中...");

        //填充攻略名称和目的地
        StrategyCatalog catalog = strategyCatalogMapper.selectByPrimaryKey(entity.getCatalog().getId());
        entity.setStrategyName(catalog.getStrategyName());
        Destination dest = new Destination();
        dest.setId(catalog.getDestId());
        entity.setDest(dest);

        if (entity.getId() == null) {
            //设置创建时间
            entity.setCreateTime(new Date());
            strategyDetailMapper.insert(entity);
            content.setId(entity.getId());
            contentMapper.insert(content);

            //让redis存储该文章的热数据
            StrategyStats vo = new StrategyStats();
            vo.setStrategyId(entity.getId());
            strategyStatsCacheService.setStrategyStats(vo);
        } else {
            strategyDetailMapper.updateByPrimaryKey(entity);
            content.setId(entity.getId());
            contentMapper.updateByPrimaryKey(content);
        }

        //2.删除旧标签关系
        strategyDetailMapper.deleteRelation(entity.getId());
        //3.保存新标签
        if (tags != null) {
            String[] ts = tags.split(",");
            for (String name : ts) {
                //查询该标签是否存在
                StrategyTag tag = strategyTagMapper.selectByName(name);
                if (tag == null) { //先新增标签
                    tag = new StrategyTag();
                    tag.setName(name);
                    strategyTagMapper.insert(tag);
                }
                //保存关系
                strategyDetailMapper.insertRelation(entity.getId(), tag.getId());
            }
        }
    }

    public String getTags(Long id) {
        return strategyTagMapper.selectByDetailId(id);
    }

    @Override
    public List<StrategyDetail> selectByDestId(Long destId) {
        return strategyDetailMapper.selectByDestId(destId);
    }

    @Override
    public List<StrategyDetail> selectByCatalogId(Long catalogId) {
        return strategyDetailMapper.selectByCatalogId(catalogId);
    }

    @Override
    public List<StrategyDetail> listViewNumTop3(Long id) {
        return strategyDetailMapper.listViewNumTop3(id);
    }

    public List<StrategyDetail> list() {
        return strategyDetailMapper.selectAll();
    }

    public void updateStats(StrategyDetail detail) {
        strategyDetailMapper.updateStats(detail);
    }
    @Override
    public PageInfo<StrategyDetail> listByUserFavor(TravelQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<StrategyDetail> list = strategyDetailMapper.listByUserFavor(qo);
        return new PageInfo<>(list);
    }

    @Override
    public void deleteIsFavor(UserInfo user, Long strategyId) {
        Long id = user.getId();
        userStrategyMapper.deleteIsFavor(id,strategyId);
    }

    @Override
    public void isFavor(UserInfo user, Long strategyId) {
        List<UserStrategy> userStrategies = userStrategyMapper.selectByUserIdAndStrategyId(user.getId(), strategyId);

        if(userStrategies.size() == 0){
            UserStrategy ut = new UserStrategy();
            ut.setUserId(user.getId());
            ut.setStrategyId(strategyId);
            userStrategyMapper.insert(ut);
        }else{
            userStrategyMapper.deleteByUserIdAndStrategyId(user.getId(), strategyId);
        }
    }
}
